package com.study.springbootsecurity.config.redis;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
 * @Description: TODO
 * @author: Huangjianyun
 * @date: 2025-06-04 17:40
 */
@Configuration
public class RedisConfig {

    @Bean
    @SuppressWarnings(value = {"unchecked", "rawtypes"})
    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object,Object> redisTemplate = new RedisTemplate();
        //创建字符串序列化作为redis key的序列化方式
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        // 设置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 创建fastJson序列化器
        FastJsonRedisSerializer serializer = new FastJsonRedisSerializer(Object.class);
        // 设置key序列化使用redis字符串序列化器
        redisTemplate.setStringSerializer(redisSerializer);
        // 设置value为 fastJson作为序列化器
        redisTemplate.setValueSerializer(serializer);

        // 设置redis hash结构 序列化器为redis字符串序列化器
        redisTemplate.setHashKeySerializer(redisSerializer);
        redisTemplate.setHashValueSerializer(serializer);

        // 初始化redisTemplate配置
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

}
